package kafkautil;

import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Properties;
import java.util.Random;
import java.util.Set;


/**
 * @author chaiduyixuan
 * @create 2023-06-18 18:52
 */

public class KafkaProducerExample {

    public static void main(String[] args) throws InterruptedException {
        String bootstrapServers = "192.168.81.133:9092";
        String topic = "stuInfo1";

        Properties props = new Properties();
        props.put("bootstrap.servers", bootstrapServers);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        try (Producer<String, String> producer = new org.apache.kafka.clients.producer.KafkaProducer<>(props)) {
            Random random = new Random();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Set<Integer> generatedIds = new HashSet<>(); // 存储已生成的学号

            while (true) {
                String[] data = generateRandomData(random, generatedIds);
                String message = String.join("\t", data);
                ProducerRecord<String, String> record = new ProducerRecord<>(topic, message);
                producer.send(record);

                System.out.println(message);

                Thread.sleep(100); // 暂停 5 秒
            }
        }
    }

    private static String[] generateRandomData(Random random, Set<Integer> generatedIds) {
        String[] data = new String[8];
        data[0] = "" + (random.nextInt(10) + 1); // 班级号
        data[1] = generateRandomName(random); // 姓名
        data[2] = random.nextInt(2) == 0 ? "0" : "1"; // 性别
        data[3] = generateRandomDate(random); // 出生年月日

        int studentId;
        do {
            studentId = random.nextInt(10000) + 1; // 学号
        } while (generatedIds.contains(studentId));

        generatedIds.add(studentId);
        data[4] = String.valueOf(studentId); // 学号

        data[5] = "" + (random.nextInt(8) + 1); // 学期
        data[6] = String.valueOf(random.nextInt(10) + 1); // 绩点
        data[7] = random.nextBoolean() ? "A" : "L"; // 在校状态

        return data;
    }

    private static String generateRandomName(Random random) {
        String[] surnames = {"Smith", "Johnson", "Brown", "Taylor", "Miller", "Wilson", "Moore", "Clark", "Walker", "Hall",
                "Allen", "Young", "King", "Wright", "Scott", "Green", "Baker", "Hill", "Carter", "Mitchell", "Turner",
                "Adams", "Morris", "Cook", "Cooper", "Perry", "Butler", "Bell", "Evans", "Reed", "Cox", "Ward", "Bailey",
                "Foster", "Barnes", "Ross", "Henderson", "Coleman", "Gray", "Brooks", "Hughes", "Price", "Bennett", "Wood",
                "Watson", "Brooks", "Harrison", "Fisher", "Russell", "Cruz", "Simmons", "Ortiz", "Jenkins", "Murray",
                "Pearson", "Reyes", "Dean", "Bryant", "Howell", "May", "Schmidt", "Bishop", "Hudson", "Spencer", "Ramos",
                "Roberts", "Baker", "Fisher", "Ryan", "Morgan", "Reid", "Marshall", "Grant", "Mason", "Perry", "Andrews",
                "Lane", "Pearson", "Andrews", "Vargas", "Chapman", "Reyes", "Dixon", "Pierce", "Berry", "Lloyd", "Carr",
                "West", "Perez", "Keller", "Lopez", "Mills", "Nichols", "James", "Cruz", "Gomez", "Caldwell", "Burns","James",
                "John", "Robert", "Michael", "William", "David", "Richard", "Charles", "Joseph",
                "Thomas", "Daniel", "Matthew", "Anthony", "Donald", "Paul", "Mark", "George", "Steven", "Edward", "Brian",
                "Ronald", "Kevin", "Jason", "Jeffrey", "Ryan", "Gary", "Nicholas", "Eric", "Stephen", "Jacob", "Larry",
                "Frank", "Jonathan", "Scott", "Justin", "Brandon", "Raymond", "Gregory", "Samuel", "Benjamin", "Patrick",
                "Jack", "Dennis", "Jerry", "Alexander", "Tyler", "Douglas", "Henry", "Peter", "Carl", "Arthur", "Nathan",
                "Albert", "Kyle", "Lawrence", "Joe", "Willie", "Gerald", "Roger", "Keith", "Jeremy", "Terry", "Harry",
                "Ralph", "Billy", "Alexander", "Roy", "Jordan", "Albert", "Ivan", "Lewis", "Wayne", "Bradley", "Clarence"};

        int index1 = random.nextInt(surnames.length);

        String name = surnames[index1];
        return name;
    }

    private static String generateRandomDate(Random random) {
        int year = random.nextInt(4) + 2001; // 2001-2004 年
        int month = random.nextInt(12) + 1; // 1-12 月
        int day = random.nextInt(28) + 1; // 1-28 日

        return String.format("%04d-%02d-%02d", year, month, day);
    }
}
